{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# chap6 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在本章中，你将学习能够将相关信息关联起来的Python字典。你将学习如何访问和修改字典中的信息。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "鉴于字典可存储的信息量几乎不受限制，因此我们会演示如何遍历字典中的数据。另外，你还将学习存储字典的列表、存储列表的字典和存储字典的字典。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "理解字典后，你就能够更准确地为各种真实物体建模。你可以创建一个表示人的字典，然后想在其中存储多少信息就存储多少信息：姓名、年龄、地址、职业以及要描述的任何方面。你还能够存储任意两种相关的信息，如一系列单词及其含义，一系列人名及其喜欢的数字，以及一系列山脉及其海拔等。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.1 一个简单的字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "green\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "alien_0 = {'color': 'green', 'points': 5}\n",
    "print(alien_0['color'])\n",
    "print(alien_0['points'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当使用字典一段时间后，你就会明白为何它可以高效地模拟现实世界中的事物。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.2 使用字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "字典是一系列的**键值对** 。每个键都与一个值相关联，你可以用键来访问与之相关的值。其中**键必须是系统定义的，值可以是任何对象**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "alien_0 = {'color': 'green'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这个字典只存储了一项有关alien_0的信息，具体地说是这个外星人的颜色。这个字典中，字符串'color'是一个键，'green'是值。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 6.2.1 访问字典中的值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要获取与键相关的值，可依次指定字典名和放在括号中的键，如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'green'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alien_0['color']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在就可以灵活使用字典了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You just earned 5 points!\n"
     ]
    }
   ],
   "source": [
    "alien_0 = {'color':'green', 'points': 5}\n",
    "new_points = alien_0['points']\n",
    "print(\"You just earned \" + str(new_points) + \" points!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 6.2.2 添加键值对"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "字典是一个动态结构，可随时添加键值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alien_0['x_position'] = 0\n",
    "alien_0['y_position'] = 25\n",
    "alien_0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 6.2.3 修改键值对"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "与添加无异"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'color': 'green', 'points': 5, 'x_position': 25, 'y_position': 25}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alien_0['x_position'] = 25\n",
    "alien_0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 删除键值对"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用del，删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'color': 'green', 'x_position': 25, 'y_position': 25}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del alien_0['points']\n",
    "alien_0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.3 遍历字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 6.3.1 遍历所有键值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "key: username\n",
      "valuebedoom\n",
      "\n",
      "key: first\n",
      "valueenrico\n",
      "\n",
      "key: last\n",
      "valuefermi\n"
     ]
    }
   ],
   "source": [
    "user_0 = {\n",
    "    'username':'bedoom',\n",
    "    'first': 'enrico',\n",
    "    'last': 'fermi',\n",
    "}\n",
    "\n",
    "for k, v in user_0.items():\n",
    "    print('\\nkey: ' + k)\n",
    "    print('value' + v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 6.3.2 遍历所有键"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " key:username\n",
      "\n",
      " key:first\n",
      "\n",
      " key:last\n"
     ]
    }
   ],
   "source": [
    "for k in user_0.keys():\n",
    "    print('\\n key:' + k)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 6.3.3 遍历所有值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " value: bedoom\n",
      "\n",
      " value: enrico\n",
      "\n",
      " value: fermi\n"
     ]
    }
   ],
   "source": [
    "for v in user_0.values():\n",
    "    print('\\n value: ' + v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.4 嵌套"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "其实就是灵活使用字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'color': 'green', 'points': 5}\n",
      "{'color': 'yellow', 'points': 10}\n",
      "{'color': 'red', 'points': 50}\n"
     ]
    }
   ],
   "source": [
    "alien_0 = {'color': 'green', 'points': 5}\n",
    "alien_1 = {'color': 'yellow', 'points': 10}\n",
    "alien_2 = {'color': 'red', 'points': 50}\n",
    "\n",
    "aliens = [alien_0, alien_1, alien_2]\n",
    "\n",
    "for alien in aliens:\n",
    "    print(alien)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "但是当我需要大量的数据时，就不可能手写。我们用循环来完成clone"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'color': 'green', 'points': 5}, {'color': 'green', 'points': 5}, {'color': 'green', 'points': 5}, {'color': 'green', 'points': 5}, {'color': 'green', 'points': 5}, {'color': 'green', 'points': 5}, {'color': 'green', 'points': 5}, {'color': 'green', 'points': 5}, {'color': 'green', 'points': 5}, {'color': 'green', 'points': 5}]\n"
     ]
    }
   ],
   "source": [
    "aliens = []\n",
    "for alien_number in range(10):\n",
    "    new_alien = {'color': 'green', 'points': 5}\n",
    "    aliens.append(new_alien)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'color': 'green', 'points': 5}\n",
      "{'color': 'green', 'points': 5}\n",
      "{'color': 'green', 'points': 5}\n",
      "{'color': 'green', 'points': 5}\n",
      "{'color': 'green', 'points': 5}\n",
      "{'color': 'green', 'points': 5}\n",
      "{'color': 'green', 'points': 5}\n",
      "{'color': 'green', 'points': 5}\n",
      "{'color': 'green', 'points': 5}\n",
      "{'color': 'green', 'points': 5}\n"
     ]
    }
   ],
   "source": [
    "for alien in aliens:\n",
    "    print(alien)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 6.4.2 字典中存储列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "没有可说的，但是**注意**虽然list也是一种数据类型，但是不能作为key，只能做value。毕竟它可变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You ordered a thick-crust pizza with the following toppings:\n"
     ]
    }
   ],
   "source": [
    "pizza = {\n",
    "    'crust': 'thick',\n",
    "    'toppings': ['mushrooms', 'extra cheese'],\n",
    "}\n",
    "\n",
    "print(\"You ordered a \" + pizza['crust'] + \"-crust pizza \" +\n",
    "    \"with the following toppings:\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 6.4.3 字典中嵌套字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Username: aeinstein\n",
      "\tFull name: Albert Einstein\n",
      "\tLocation: Princeton\n",
      "\n",
      "Username: mcurie\n",
      "\tFull name: Marie Curie\n",
      "\tLocation: Paris\n"
     ]
    }
   ],
   "source": [
    "users = {\n",
    "    'aeinstein': {\n",
    "        'first': 'albert',\n",
    "        'last': 'einstein',\n",
    "        'location': 'princeton',\n",
    "    },\n",
    "    'mcurie': {\n",
    "        'first': 'marie',\n",
    "        'last': 'curie',\n",
    "        'location': 'paris',\n",
    "    },\n",
    "}\n",
    "for username, user_info in users.items(): \n",
    "    print(\"\\nUsername: \" + username)\n",
    "    full_name = user_info['first'] + \" \" + user_info['last']\n",
    "    location = user_info['location']\n",
    "    print(\"\\tFull name: \" + full_name.title())\n",
    "    print(\"\\tLocation: \" + location.title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.5 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在本章中，你学习了：\n",
    "1. 如何定义字典，以及如何使用存储在字典中的信息；\n",
    "2. 如何访问和修改字典中的元素，以及如何遍历字典中的所有信息；\n",
    "3. 如何遍历字典中所有的键-值对、所有的键和所有的值；\n",
    "4. 如何在列表中嵌套字典、在字典中嵌套列表以及在字典中嵌套字典。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
